package com.pengtu.gsj.service.impl.check.lwfw;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.yh.MainTenanceDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.yh.MainTenance;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lwfw.MainTenanceWorkCheckService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class MainTenanceWorkCheckServiceImpl implements MainTenanceWorkCheckService{

	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public MainTenanceDao mainTenanceDao;
	
	@Override
	public Map<String, Object> getMainTenanceWorkChecks(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<String> attCheckLists = Arrays.asList("0", "0");;
		List<MainTenance> mianLists = new ArrayList<MainTenance>();
		List<Double> scoLists = new ArrayList<Double>();
		List<List<String>> resultLists = new ArrayList<List<String>>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String itemNameIds = (String)jsonObject.get("itemProjectId");
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double tableFull = yearFull * 4;
		double percentage = 0; //完善率
		double total = 0; //计算总分
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		if (StringUtils.isNotEmpty(itemNameIds)) {
			//通过逗号将每个id切割出来，并搜索算分
			String[] idArrs = itemNameIds.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				//根据tag检索是否有附件
				if( i > 3) {
					break;
				}
			//根据ID查询获得养护作业的数据
			MainTenance mainTenance = mainTenanceDao.get(idArrs[i]);
			mianLists.add(mainTenance);
			if (null != mainTenance.getAttachments()){
				double scoTemp = 0;
				attCheckLists = Arrays.asList("0", "0");
				for (Attachment attachment : mainTenance.getAttachments()) {
					if ("yhzy_zyjh".equals(attachment.getTag()) && "0".equals(attachment.getDelFlag()) && "0" == attCheckLists.get(0)) {
						attCheckLists.set(0, "1");
					} else if ("yhzy_jtzzfa".equals(attachment.getTag()) && "0".equals(attachment.getDelFlag()) && "0" == attCheckLists.get(1))  {
						attCheckLists.set(1, "1");
					}
				}
				resultLists.add(attCheckLists);
				//遍历查询附件结果，算分
				scoTemp = yearFull;
				for (String str : attCheckLists) {
					if ("0".equals(str)) {
						scoTemp -= yearPart;
					}
				}
				scoTemp = scoTemp < 0 ? 0 : scoTemp;
				scoLists.add(scoTemp);
				total += scoTemp;
				map.put("flag", true);
				}
				percentage = MathsUtils.divDouble(total*100, tableFull, 2);
			}
		}
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("manageData", mianLists);
		map.put("scoLists", scoLists);
		map.put("percent", percentage);
		map.put("resultLists", resultLists);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
	

	/**
	 * 检索养护作业的项目列表
	 * @param startTime:项目开始时间 
	 * @param startTime:项目结束时间
	 */
	@Override
	public List<MainTenance> getMainTenanceWorkCheckProject(QueryFilter queryFilter) {
		return mainTenanceDao.find(queryFilter, "projectYear", true);
	}
}